package org.i_plus.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i_plus.model.HobbyBean;
import org.i_plus.model.MemberBean;


public class MemberServlet extends HttpServlet {
//private MemberService service;
//	public void init() throws ServletException {
//		WebApplicationContext context =
//				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		this.service = (MemberService) context.getBean("MemberService");		
//	}
	int hi,mlikeInt;
	boolean te15;
	java.sql.Date sdate;
	@Override
	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		//前端丟資料過來！接！
		String temp1=request.getParameter("account");
		String temp2=request.getParameter("pwd");
		String temp3=request.getParameter("name");
		String temp4=request.getParameter("nickname");
		String temp7=request.getParameter("sex");
		String year=request.getParameter("yy");
		String month=request.getParameter("mm");
		String day=request.getParameter("dd");
		String temp8=year+"-"+month+"-"+day;
		String temp9=request.getParameter("hight");
		String temp10=request.getParameter("region");
		String temp11=request.getParameter("blood");
		String temp12=request.getParameter("state");
		String temp14=request.getParameter("mlike");
		String temp15=request.getParameter("play");
		//以下是抓「興趣」的值！
		String code1=request.getParameter("ball");
		String code2=request.getParameter("run");
		String code3=request.getParameter("reading");
		String code4=request.getParameter("swim");
		String code5=request.getParameter("movie");
		String code6=request.getParameter("singing");
		String code7=request.getParameter("bike");
		String code8=request.getParameter("tvgame");
		String pro=request.getParameter("inp");
		
		//request.setAttribute("aa", "bb");要拿的時候request.get...("aa")就能拿到bb！但記得強轉！
		//String a = (String)request.getAttribute("cc");
//		HttpSession session = request.getSession();
//		session.setAttribute("AA", "BB");
		
		//看是否有未填的必填欄位
		Map<String,String> errors=new HashMap<String,String>();
		request.setAttribute("errors", errors);
		if(pro!=null){
			if(pro.equals("next")){
				if(temp1==null || temp1.length()==0)
				{
					errors.put("account","請輸入帳號，必須是E-mail");
				}
				if(temp3==null || temp3.length()==0)
				{
					errors.put("name","請輸入真實姓名");
				}
				if(temp4==null || temp4.length()==0)
				{
					errors.put("nickname","請輸入顯示暱稱");
				}
				if(temp7==null || temp7.length()==0)
				{
					errors.put("sex","請選擇性別");
				}
				if(temp2==null || temp2.length()==0)
				{
					errors.put("pwd","請輸入密碼");
				}
				if(temp9==null || temp9.length()==0)
				{
					hi=0;
				}
				else {
					int hi=Integer.parseInt(temp9);
					if(hi>250 || hi<0)
					{
						errors.put("hight","請輸入正常數字");
					}
					this.hi=hi;
				}
				if(temp11==null || temp11.length()==0)
				{
					temp11="N";
				}
				if(temp15==null || temp15.length()==0)
				{
					te15=false;
				}
				else{
				boolean te15=temp15.contentEquals(temp15);
				this.te15=te15;
				}
				if(temp14==null || temp14.length()==0)
				{
					mlikeInt=0;
				}
				else {
					int mlikeInt=Integer.parseInt(temp14);
				}
			}
		}
		byte[] pwd=temp2.trim().getBytes();
		if(year==null || year.length()==0 || month==null || month.length()==0 || day==null || day.length()==0){
			errors.put("bdate", "請點選生日");
		}else{
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
		try {
			sdate = java.sql.Date.valueOf(temp8);
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
		if(errors!=null && !errors.isEmpty()){
			request.getRequestDispatcher("member.jsp").
			forward(request, response);
			return;
		}
		//轉換「興趣」！從字串轉true or false
		boolean bcode1;
		boolean bcode2;
		boolean bcode3;
		boolean bcode4;
		boolean bcode5;
		boolean bcode6;
		boolean bcode7;
		boolean bcode8;
		if(code1==null)
		{
			bcode1=false;
		}
		else{
			bcode1=true;
		}
		if(code2==null)
		{
			bcode2=false;
		}
		else{
			bcode2=true;
		}
		if(code3==null)
		{
			bcode3=false;
		}
		else{
			bcode3=true;
		}
		if(code4==null)
		{
			bcode4=false;
		}
		else{
			bcode4=true;
		}
		if(code5==null)
		{
			bcode5=false;
		}
		else{
			bcode5=true;
		}
		if(code6==null)
		{
			bcode6=false;
		}
		else{
			bcode6=true;
		}
		if(code7==null)
		{
			bcode7=false;
		}
		else{
			bcode7=true;
		}
		if(code8==null)
		{
			bcode8=false;
		}
		else{
			bcode8=true;
		}
		//個人資料塞進memberbean裡！
		MemberBean bean=new MemberBean();
		bean.setAccount(temp1);
		bean.setPwd(pwd);
		bean.setName(temp3);
		bean.setNickname(temp4);
		bean.setSex(temp7);
		bean.setBdate(sdate);
		bean.setHight(hi);
		bean.setRegion(temp10);
		bean.setState(temp12);
		bean.setBlood(temp11);
		bean.setMlike(mlikeInt);
		bean.setPop(1);
		bean.setPlay(te15);
		bean.setBonus(1);
		bean.setMkind("a");
		bean.setCheckvip(sdate);
		//個人興趣塞進hobbybean裡！
		HobbyBean hbean=new HobbyBean();
     	hbean.setAccount(bean.getAccount());
		hbean.setBall(bcode1);
		hbean.setRun(bcode2);
		hbean.setReading(bcode3);
		hbean.setSwim(bcode4);
		hbean.setMovie(bcode5);
		hbean.setSinging(bcode6);
		hbean.setBike(bcode7);
		hbean.setTvgame(bcode8); 
		
//		WebApplicationContext context =
//				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
//		sessionFactory.getCurrentSession().beginTransaction();
		if(pro.equals("next")){
			HttpSession ses = request.getSession();
			ses.setAttribute("presendMember", bean);
			ses.setAttribute("presendHobby", hbean);
			System.out.println(bean);
			System.out.println(hbean);
		request.getRequestDispatcher("/member2.jsp").
		forward(request, response);
		return;
				}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
